﻿namespace UFIDA.U9.Cust.U9Demo.DeliveryBP
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Util.Context;
    using System.Linq;

    /// <summary>
    /// CreateDocFromPO partial 
    /// </summary>	
    public partial class CreateDocFromPO 
	{	
		internal BaseStrategy Select()
		{
			return new CreateDocFromPOImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class CreateDocFromPOImpementStrategy : BaseStrategy
	{
		public CreateDocFromPOImpementStrategy() { }

		public override object Do(object obj)
		{						
			CreateDocFromPO bpObj = (CreateDocFromPO)obj;

            //get business operation context is as follows
            //IContext context = ContextManager.Context	

            //auto generating code end,underside is user custom code
            //and if you Implement replace this Exception Code...
            if (bpObj == null)
                return null;
            long resultID = 0;
            if(bpObj.POShipLineList == null ||bpObj.POShipLineList.Count == 0)
            {
                throw new Exception("请选择采购计划行数据");
            }
            //使用Session创建实体
            using (ISession session = Session.Open())
            {
                
                DeliverBE.DocType docType = null;
                if (bpObj.DocumentType > 0)
                {
                    docType = DeliverBE.DocType.Finder.FindByID(bpObj.DocumentType);
                }
                if (docType == null)
                {
                    docType = DeliverBE.DocType.Finder.Find("Org=" + UFIDA.U9.Base.Context.LoginOrg.ID + " and IsCreatedByPush =1");
                }
                if (docType == null)
                {
                    throw new Exception("请选择单据类型，或设置单据类型“上游单据推出");
                }
               
                
                UFIDA.U9.PM.PO.POShipLine.EntityList poshipLineList = UFIDA.U9.PM.PO.POShipLine.Finder.FindAll(string.Format("ID in ({0})",String.Join(",", bpObj.POShipLineList.ToArray())));
                //这里可以使用Linq语句（可以百度使用方法）根据供应商拆单生成不同送货单；或者校验不能选择不同的供应商
                var supplierGrouop = poshipLineList.Cast<UFIDA.U9.PM.PO.POShipLine>().GroupBy(p => p.POLine.PurchaseOrder.Supplier.Supplier.ID);
                foreach(var supplierList in supplierGrouop)
                {
                    //除非百分百确认Poline/PurchaseOrder/Supplier不为空，否则需要做空校验
                    UFIDA.U9.PM.PO.POShipLine fistPOShipLine = supplierList.First();
                    UFIDA.U9.CBO.SCM.Supplier.Supplier supplier = fistPOShipLine.POLine.PurchaseOrder.Supplier.Supplier;
                    //创建送货单表头实体
                    UFIDA.U9.Cust.U9Demo.DeliverBE.Delivery delivery = UFIDA.U9.Cust.U9Demo.DeliverBE.Delivery.Create();
                    if (resultID == 0)
                        resultID = delivery.ID;
                    delivery.DocumentType = docType;
                    delivery.Supplier = supplier;
                    delivery.Currency = supplier.TradeCurrency;
                    delivery.OwnOrg = fistPOShipLine.POLine.PurchaseOrder.Org;
                    delivery.BusinessDate = DateTime.Now;
                    int lineNo = 10;
                    foreach (UFIDA.U9.PM.PO.POShipLine poshipLine in poshipLineList)
                    {
                        //创建送货单表体实体
                        DeliverBE.DeliveryLine line = DeliverBE.DeliveryLine.Create(delivery);
                        line.DocLineNo = lineNo;
                        line.Item = poshipLine.ItemInfo.ItemID;
                        line.Qty = poshipLine.ReqQtyTU;
                        line.Price = poshipLine.POLine.FinallyPriceTC;
                        line.Uom = poshipLine.PriceUOM;
                        line.SrcPOShipLine = poshipLine;
                        line.Amount = line.Qty * line.Price;
                        lineNo += 10;
                    }
                }
               
                session.Commit();
            }
            //只返回第一条创建的送货单ID
             return resultID;
		}		
	}

	#endregion
	
	
}